"""
管理员视图层
"""

from interface import admin_interface
from interface import common_interface
from lib import common

# 记录当前用户登录状态
admin_info = {
    'user' : None
}

# 注册功能
def register():
    while True:
        username = input('请输入您的用户名：').strip()
        password = input('请输入密码：').strip()
        re_password = input('请确认密码：').strip()

        # 小的逻辑判断
        if password == re_password:
            # 调用管理员的注册接口层
            flag, msg = admin_interface.admin_register_interface(
                username, password
            )

            if flag:
                print(msg)
                break

            else:
                print(msg)
        else:
            print('两次密码输入不一致！请重新输入~~~')

# 登录功能
def login():
    while True:
        username = input('请输入您的用户名：').strip()
        password = input('请输入密码：').strip()

        # 调用公共接口校验
        flag, msg = common_interface.login_interface(
            username, password, user_type= 'admin'
        )

        if flag:
            print(msg)
            # 记录当前用户登录状态
            admin_info['user'] = username

            break
        else:
            print(msg)

# 创建学校功能
@common.auth('admin')
def create_school():
    while True:
        school_name = input('请输入创建学校的名称：').strip()
        school_addr = input('请输入创建学校的地址：').strip()

        # 调用管理员创建学校接口
        flag, msg = admin_interface.create_school_interface(
            school_name, school_addr, admin_info['user']
        )

        if flag:
            print(msg)
            break

        else:
            print(msg)

# 创建课程功能
@common.auth('admin')
def create_course():
    while True:
        # 查看当前所有创建的学校
        flag, school_list_or_msg = common_interface.get_all_school_interface()
        if not flag:
            print(school_list_or_msg)
            break

        for index, school_name in enumerate(school_list_or_msg):
            print(f'学校编号：[{index}]   学校名称：[{school_name}]')

        choice = input('请输入选择的学校编号：').strip()

        if not choice.isdigit():
            print('请输入数字！')
            continue

        choice = int(choice)

        if choice not in range(len(school_list_or_msg)):
            print('请输入正确的编号！')
            continue

        school_name = school_list_or_msg[choice]


        course_name = input('请输入创建的课程名称：').strip()

        # 调用创建课程接口层
        flag, msg = admin_interface.create_course_interface(
            school_name, course_name, admin_info['user']
        )

        if flag:
            print(msg)
            break

        else:
            print(msg)

# 创建讲师功能
@common.auth('admin')
def create_teacher():
    while True:
        teacher_name = input('请输入创建的讲师名字：').strip()

        # 调用创建课程接口层
        flag, msg = admin_interface.create_teacher_interface(
            teacher_name, admin_info['user']
        )

        if flag:
            print(msg)
            break

        else:
            print(msg)


# 函数字典
func_dic = {
    '1': register,
    '2': login,
    '3': create_school,
    '4': create_course,
    '5': create_teacher,
}


# 功能函数
def admin_view():
    while True:
        print('''
        ========欢迎来到管理员视图层=========
                    - 1.注册
                    - 2.登录
                    - 3.创建学校
                    - 4.创建课程
                    - 5.创建讲师
        ============24小时为您服务==========
        ''')

        choice = input('请选择功能编号（输入q退出）: ').strip()

        if choice == 'q':
            break

        if not choice.isdigit():
            print('请输入数字！')
            continue

        if choice not in func_dic:
            print('请输入正确的编号！')
            continue

        func_dic.get(choice)()
